package com.ifast.data.controller;


import java.util.Arrays;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.ifast.data.domain.DataCityDO;
import com.ifast.data.domain.DataStrokeDO;
import com.ifast.data.service.DataStrokeService;
import com.ifast.common.type.EnumErrorCode;
import com.ifast.common.utils.ImportExcelUtil;
import com.ifast.common.utils.Result;
import com.ifast.common.utils.StringUtils;

/**
 * 
 * <pre>
 * 行程信息
 * </pre>
 * <small> 2018-04-15 21:40:23 | lvyq</small>
 */
@Controller
@RequestMapping("/data/dataStroke")
public class DataStrokeController {
	@Autowired
	private DataStrokeService dataStrokeService;
	
	@GetMapping()
	@RequiresPermissions("data:dataStroke:dataStroke")
	String DataStroke(){
	    return "data/dataStroke/dataStroke";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("data:dataStroke:dataStroke")
	public Result<Page<DataStrokeDO>> list(Integer pageNumber, Integer pageSize, DataStrokeDO dataStrokeDTO){
		// 查询列表数据
        Page<DataStrokeDO> page = new Page<>(pageNumber, pageSize);
        
        Wrapper<DataStrokeDO> wrapper = new EntityWrapper<DataStrokeDO>(dataStrokeDTO);
        page = dataStrokeService.selectPage(page, wrapper);
        int total = dataStrokeService.selectCount(wrapper);
        page.setTotal(total);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("data:dataStroke:add")
	String add(){
	    return "data/dataStroke/add";
	}

	@GetMapping("/edit/{strokeId}")
	@RequiresPermissions("data:dataStroke:edit")
	String edit(@PathVariable("strokeId") Long strokeId,Model model){
		DataStrokeDO dataStroke = dataStrokeService.selectById(strokeId);
		model.addAttribute("dataStroke", dataStroke);
	    return "data/dataStroke/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("data:dataStroke:add")
	public Result<String> save( DataStrokeDO dataStroke){
		Long id = dataStrokeService.getId()+1;
		System.out.println(id);
		dataStroke.setStrokeId(id);
		if(dataStrokeService.insert(dataStroke)){
			return Result.ok();
		}
		return Result.fail();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("data:dataStroke:edit")
	public Result<String>  update( DataStrokeDO dataStroke){
		dataStrokeService.updateAllColumnById(dataStroke);
		return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("data:dataStroke:remove")
	public Result<String>  remove( Long strokeId){
		if(dataStrokeService.deleteById(strokeId)){
            return Result.ok();
		}
		return Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("data:dataStroke:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] strokeIds){
		dataStrokeService.deleteBatchIds(Arrays.asList(strokeIds));
		return Result.ok();
	}
	
	@GetMapping("/api/v1/getStroke")
	@ResponseBody
	public Result<List<DataStrokeDO>> getStroke(DataStrokeDO dataStroke) {
		Wrapper<DataStrokeDO> wrapper = new EntityWrapper<DataStrokeDO>(dataStroke);
		List<DataStrokeDO> StrokeList = dataStrokeService.selectList(wrapper);
		if(StrokeList!=null&&StrokeList.size()>0) {
			return Result.ok(StrokeList);
		}
		return Result.ok();
	}
	
	
	@GetMapping("/api/v1/getStroke2")
	@ResponseBody
	public JSONPObject getStroke(String jsonpCallback,DataStrokeDO dataStroke) {
		Wrapper<DataStrokeDO> wrapper = new EntityWrapper<DataStrokeDO>(dataStroke);
		List<DataStrokeDO> StrokeList = dataStrokeService.selectList(wrapper);
		if(StrokeList!=null&&StrokeList.size()>0) {
			return new JSONPObject(jsonpCallback, Result.ok(StrokeList));
		}
		return new JSONPObject(jsonpCallback, Result.ok());
	}
	
	
	@GetMapping("/batchImport")
	@RequiresPermissions("data:dataStroke:batchImport")
	String batchImport(){
	    return "data/dataStroke/batchImport";
	}
	
	@ResponseBody
	@PostMapping("/uploadExcel")
	public Result<String> uploadExcel(@RequestParam("file") MultipartFile file) {
		try {
			List<List<Object>> list = new ImportExcelUtil().getBankListByExcel(file.getInputStream(),
					file.getOriginalFilename(), 1);
			for (int i = 0; i < list.size(); i++) {
				System.out.println(list.get(i));
				DataStrokeDO ds = dataStrokeService.selectById(StringUtils.isNull(list.get(i).size()>=1?list.get(i).get(0)+"":""));
				if(ds!=null) {
					ds.setDeparture(StringUtils.isNull(list.get(i).size()>=2?list.get(i).get(1)+"":""));
					ds.setDestination(StringUtils.isNull(list.get(i).size()>=3?list.get(i).get(2)+"":""));
					ds.setPrice(StringUtils.isNull(list.get(i).size()>=4?list.get(i).get(3)+"":""));
					ds.setDiscountInfo(StringUtils.isNull(list.get(i).size()>=5?list.get(i).get(4)+"":""));
					ds.setTransportSchedule(StringUtils.isNull(list.get(i).size()>=6?list.get(i).get(5)+"":""));
					ds.setRemarks(StringUtils.isNull(list.get(i).size()>=7?list.get(i).get(6)+"":""));
					//ds.setDepartureAddress(StringUtils.isNull(list.get(i).get(7)+""));
					//ds.setDestinationAddress(StringUtils.isNull(list.get(i).get(8)+""));
					dataStrokeService.updateById(ds);
				}else {
					DataStrokeDO dataStrokeDO=new DataStrokeDO();
					dataStrokeDO.setStrokeId(Long.valueOf(StringUtils.isNull(list.get(i).size()>=1?list.get(i).get(0)+"":"")));
					dataStrokeDO.setDeparture(StringUtils.isNull(list.get(i).size()>=2?list.get(i).get(1)+"":""));
					dataStrokeDO.setDestination(StringUtils.isNull(list.get(i).size()>=3?list.get(i).get(2)+"":""));
					dataStrokeDO.setPrice(StringUtils.isNull(list.get(i).size()>=4?list.get(i).get(3)+"":""));
					dataStrokeDO.setDiscountInfo(StringUtils.isNull(list.get(i).size()>=5?list.get(i).get(4)+"":""));
					dataStrokeDO.setTransportSchedule(StringUtils.isNull(list.get(i).size()>=6?list.get(i).get(5)+"":""));
					dataStrokeDO.setRemarks(StringUtils.isNull(list.get(i).size()>=7?list.get(i).get(6)+"":""));
					//dataStrokeDO.setDepartureAddress(StringUtils.isNull(list.get(i).get(7)+""));
					//dataStrokeDO.setDestinationAddress(StringUtils.isNull(list.get(i).get(8)+""));
					dataStrokeService.insert(dataStrokeDO);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(EnumErrorCode.FileUploadGetBytesError.getCode(),
					EnumErrorCode.FileUploadGetBytesError.getMsg());
		}
		return Result.ok();
	}
	
}
